< previous page page_540 next page >

Page 540
Listing 14.6. continued
LocalErr:
    If Err.Number = 6 Then ' was it an overflow error?
        strMsg = "You have entered an invalid integer value." & vbCrLf
        strMsg = strMsg & "The program will now set the value to 0 for you." &
arrow.gifvbCrLf
        strMsg = strMsg & "Select YES to set the value to 0 and continue." &
arrow.gifvbCrLf
        strMsg = strMsg & "Select NO to return to enter a new value."
        '
        lngReturn = MsgBox(strMsg, vbCritical + vbYesNo, "cmdResumeNextErr")
        If lngReturn = vbYes Then
            intValue = 0
            Resume Next
        Else
            Resume
        End If
    Else ' must have been some other error(!)
        strMsg = CStr(Err.Number) & " - " & Err.Description
        MsgBox strMsg, vbCritical, "cmdResumeNext"
        Resume
    End If
    '
LocalExit:
    '
End Sub
In Listing 14.6, you added a section of code to the error handler that tests for the anticipated overflow error. You explain the options to the user and then give the user a choice of how to proceed. This is a good general model for error handling that involves user interaction. Tell the user the problem, explain the options, and let the user decide how to go forward.
Notice, also, that this routine includes a general error trap for those cases when the error is not caused by an integer overflow. Even when you think you have covered all the possible error conditions, you should always include a general error trap.
Save and run this project. When you press the Next command button and enter an invalid value (that is, any number greater than 32,767), you see the error message that explains your options (see Figure 14.5).
Using Resume label to Exit an Error Handler
There are times when you need your program to return to another spot within the routine in order to fix an error that occurs. For example, if you ask the user to enter two numbers that you use to perform a division operation, and it results in a divide-by-zero error, you want to ask the user to enter both numbers again. You might not be able to just use the Resume statement after you handle the error.

 
< previous page page_540 next page >

If you like this book, buy it!